import { Card,Button, Checkbox, Form, Input, Space ,message} from 'antd'
import React, { useEffect } from 'react'
import {useNavigate,useParams}from 'react-router-dom'
import {reqAddHospitalSet,
  reqGetHospitalSet,
  reqUpdateHospitalSet,}from '@api/user/hospital/hospitalSet'
export default function AddOrUpdateHospitalSet() {
  const navigate = useNavigate()
  const [form] = Form.useForm()
   const { id } = useParams()
   



    useEffect(()=>{

      async function fetch(){
        
        if(id){
          const result=await reqGetHospitalSet(id)
              console.log(result)
          form.setFieldsValue(result)
        }
      }
      fetch()
    },[])




        const onFinish = async (values: any) => {

          if(id){
            values.id = id
            await reqUpdateHospitalSet(values)
            message.success('修改成功')
          }
          else{
            await reqAddHospitalSet(values)
            message.success('添加成功')
          }
          navigate('/syt/hospital/hospitalSet')
        };
      
        // const onFinishFailed = (errorInfo: any) => {
        //   console.log('Failed:', errorInfo);
        // };


  return (
    <Card>

<Form
form={form}
      name="basic"
      labelCol={{ span: 2 }}
      wrapperCol={{ span: 22 }}
      initialValues={{ remember: true }}
      onFinish={onFinish}
      // onFinishFailed={onFinishFailed}
      autoComplete="off"
    >
      <Form.Item
        label="医院名称"
        name="hosname"
        rules={[{ required: true, message: 'Please input your username!' }]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="医院编码"
        name="hoscode"
        rules={[{ required: true, message: 'Please input your password!' }]}
      >
          <Input />

      </Form.Item>

      <Form.Item
        label="api路径"
        name="apiUrl"
        rules={[{ required: true, message: 'Please input your password!' }]}
      >
                <Input />

      </Form.Item>

      <Form.Item
        label="签名"
        name="signKey"
        rules={[{ required: true, message: 'Please input your password!' }]}
      >
               <Input />

      </Form.Item>

      <Form.Item
        label="联系人姓名"
        name="contactsName"
        rules={[{ required: true, message: 'Please input your password!' }]}
      >
               <Input />

      </Form.Item>

      <Form.Item
        label="联系人手机"
        name='contactsPhone'
        rules={[{ required: true, message:  "请输入合法的联系人手机",
        pattern: /^1[3-9][0-9]{9}$/, // 正则表达式
     }]}
      >
               <Input />

      </Form.Item>


      <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
       <Space>
       <Button type="primary" htmlType="submit">
          保存
        </Button>
        <Button type="default" onClick={()=>{
         navigate(-1)
        }}>
          返回
        </Button>
       </Space>
      </Form.Item>
    </Form>


    </Card>
  )
}
